/*!
 * 滚动条控件.
 * ecp.qzz.ui.scrollbar.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.window"], function() {
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 控件类
	 */
	qzz.ui.scrollbar = qzz.Class(qzz.ui.window, {
		/**
		 * 创建函数.
		 */
		_create:function(){			
			this.base();	
			this.className = "ScrollBar";
			this.logInfo("scrollBar.create");
		    this.visible = true;
		    //控件宽高
		    this.getOption().height = 132;
		    this.getOption().width = 17;
		    //滚动坐标
		    this.scrollIndex = 0;
		    //滚动数量
		    this.scrollCount = 0;
		    //可见数量
		    this.visibleCount = 0;
		    //单位步长
		    this.stepWidth = 5;
	    },
	    /**
	     * 渲染控件
	     */
	    _render:function(){
	    	this.base();
	    	this.thisWindow.style.overflowY = "scroll";
	    	this._innerDiv = this.createElement("DIV");
	    	this.thisWindow.appendChild(this._innerDiv);
	    	this.thisWindow.style.borderWidth = "0px";
	    	this._innerDiv.style.height = "10px";
	    	this._innerDiv.style.width = "1px";
	    	var _this = this;
	    	this.thisWindow.onscroll = function(ev) {
	    		_this._onScroll(ev, this.scrollHeight - _this.getHeight(), this.scrollTop);
	    	}
	    },
	    /**
	     * 滚动事件.
	     */
	    _onScroll:function(ev, height, top) {
	    	if(this._update) {
		    	this.scrollIndex = Math.round(top/this.stepWidth,10);
		    	if(typeof this.onScroll == "function") {
	    			this.onScroll(this.scrollIndex, this.scrollIndex - this.priorScrollIndex);
	    		}
		    	this.priorScrollIndex = this.scrollIndex;
	    	}
	    },
	    /**
	     * 刷新控件.
	     */
	    refresh : function(){	    	
	    	var sh = (this.scrollCount - this.visibleCount) * this.stepWidth + this.getHeight();
	    	if(sh <= 0) {
	    		sh = 1;
	    	}
	    	this._innerDiv.style.height = sh + "px";
	    	if(sh > this.getOption().height && this.thisWindow.clientWidth > 1) {
	    		this.getOption().width = this.getOption().width - this.thisWindow.clientWidth + 1;
	    	}
	    	this.thisWindow.style.width = this.getOption().width + "px";
	    },
	    /**
	     * 设置显示状态.
	     * @param value 状态
	     */
	    setVisible:function(value) {
	    	if(value) {
	    		this.thisWindow.style.display = "";
	    	} else {
	    		this.thisWindow.style.display = "none";
	    	}
	    	this.visible = value;
	    },
	    /**
	     * 返回显示状态.
	     */
	    getVisible:function() {
	    	return this.thisWindow.style.display == "";
	    },
	    getWidth:function() {
	    	return this.getOption().width + 1;
	    },
	    /**
	     * 设置高度.
	     * @param height 高度
	     */
	    setHeight:function(height) {
	    	height = height + 2;
	    	if(height < 34) {
	    		height = 34;
	    	}
	    	this.base(height);
	    	this.refresh();
	    },
	    /**
	     * 设置滚动位置.
	     * @param index 位置号
	     */
	    setScrollIndex:function(index, triggerEvent){
	    	if(index == this.scrollIndex) return;
	    	var scrollCount = index - this.scrollIndex;
	    	this.scrollIndex = index;
	    	this.thisWindow.onscroll = null;
	    	this.thisWindow.scrollTop = Math.round(index * this.stepWidth, 10);
	    	//手工调用。
	    	if(triggerEvent !== false) {
	    		this._onScroll(null, this.thisWindow.scrollHeight - this.getHeight(), this.thisWindow.scrollTop);
	    	} else {
	    		this.priorScrollIndex = this.scrollIndex;
	    	}
	    	var _this = this;
    		_this.thisWindow.onscroll = function(ev) {
	    		_this._onScroll(ev, this.scrollHeight - _this.getHeight(), this.scrollTop);
	    	}
	    },
	    /**
	     * 设置滚动数量.
	     * @param count 数量
	     */
	    setScrollCount:function(count, vcount) {
	    	if(count < 0) {
	    		count = 0;
	    	}
	    	if(this.scrollCount === count && this.visibleCount === vcount) return;
	    	this.scrollCount = count;
	    	this.visibleCount = vcount;
	    	scrollCount = 0;
	    	if(this.scrollIndex > count - vcount) {
	    		scrollCount = (count - vcount) - this.scrollIndex;
	    		this.scrollIndex = count - vcount;
	    	}
	    	//刷新状态
	    	this.refresh();
	    	if(typeof this.onScroll == "function") {
    			this.onScroll(this.scrollIndex, scrollCount);
    		}
	    	this.priorScrollIndex = this.scrollIndex;
	    },
	    /**
	     * 向前滚动.
	     * @param time 滚动时间
	     */
	    scrollPrior:function(time){
	    	var re = false;
	    	if(this.scrollIndex > 0) {
	    		this.setScrollIndex(this.scrollIndex-1);
	    		re = true;
	    	}
	    	return re;
	    },
	    /**
	     * 向后滚动
	     * @param time 滚动时间.
	     */
	    scrollNext:function(time){
	    	var re = false;
	    	if(this.scrollIndex < this.scrollCount - this.visibleCount) {
	    		this.setScrollIndex(this.scrollIndex+1);
	    		re = true;
	    	}
	    	return re;
	    }
	});
	_bindRenderFunc("qzzscroll", qzz.ui.scrollbar);
	return qzz.ui.scrollbar;
});
	